//----------------------------------------------------------
// Timer.h
//   Timer class implementation for HUD displays or background
//      management, can run ascending / descending
//   Should be updated with the engine frametime / deltatime
//----------------------------------------------------------

#include "Timer.h"

Timer::Timer()
{
	_timerOn = false;
	_timeMin = 0;
	_timeSec = 0;
	_incrTime = 0.0f;
	_timeToCount = 0.0f;
}

Timer::~Timer()
{
	// do nothing
}

bool Timer::Initialize(float timeToCount, bool countdown)
{
	// initialize timer
	//  if not initialized, does nothing

	_timeToCount = timeToCount;
	_countdown = countdown;

	if (_countdown)
	{
		_incrTime = timeToCount;
	}
	else
	{
		_incrTime = 0.0f;
	}

	ConvertTime(_incrTime);

	return true;
}

bool Timer::Update(float deltaTime)
{
	// update the counter depending on the type of countdown
	if (_countdown) 
	{
		_incrTime -= deltaTime;
		// clamp to 0 if timer goes under zero
		if (_incrTime < 0) { _incrTime = 0; }
	}
	else
	{	
		_incrTime += deltaTime;
		// clamp to timeToCount if timer goes over timeToCount
		if (_incrTime > _timeToCount) { _incrTime = _timeToCount; }	
	}

	// recalc the min/sec portions of the timer
	ConvertTime(_incrTime);

	return true;
}

void Timer::ConvertTime(float incrTime)
{
	// recalc the min/sec portions of the timer
	_timeMin = static_cast<int>(incrTime / 60000);
	int timeMod = static_cast<int>(incrTime) % 60000;
	_timeSec = timeMod / 1000;
}